# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Eric Bélanger <eric@archlinux.org>

_name=Audacity
pkgbase=audacity
pkgname=(
  audacity
  audacity-docs
)
pkgver=3.3.1
pkgrel=2
epoch=1
pkgdesc="A program that lets you manipulate digital audio waveforms"
arch=(loong64 x86_64)
url="https://audacityteam.org"
license=(GPL3)
makedepends=(
  alsa-lib
  chrpath
  cmake
  ffmpeg
  flac
  gcc-libs
  glibc
  gdk-pixbuf2
  glib2
  gtk3
  jack
  lame
  libid3tag
  libmad
  libogg
  libsbsms
  libsndfile
  libsoxr
  libvorbis
  lilv
  lv2
  mpg123
  portaudio
  portmidi
  portsmf
  python
  soundtouch
  sqlite
  suil
  twolame
  wavpack
  wxwidgets-gtk3
  vamp-plugin-sdk
  vst3sdk
)
options=(!lto)
source=(
  https://github.com/audacity/audacity/archive/refs/tags/$_name-$pkgver.tar.gz
  https://github.com/$pkgbase/$pkgbase/releases/download/$_name-$pkgver/$pkgbase-manual-v$pkgver.tar.gz
)
sha512sums=('741b74799652cc3ad915eec3d8189cd7c2f52838a4927be1fe3303b8984f7a2975e1b39f2b753b7417a56cd0899213de1a13327ed5fbdc57589516daebe0c6e9'
            '4aa7ab79cba95bdc8ea17c7c4c07eb10bf7ba1bbebc37191400d6e3cad7de674a182f73446406ea51186f00352a69eae99fe023f401ebc4aa4f09d256231affe')
b2sums=('06c1a718cfe81cb0959f061fc0e7eadbb07334479072be631a5ffb663d89b47dd5c36de500ccaac0db68cb2799e2480e8ad8cb62ae5426e89710eaeee70f4618'
        'd93b56dbfd9ec2520808a67e47e5823453a6e3615c41ebab6c4238dc9caf548ed2b907454006a3dff6a0f3ac02aba5bc765433ad67cd2ebff01afd96f773230e')

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D AUDACITY_BUILD_LEVEL=2
    -D audacity_conan_enabled=OFF
    -D audacity_has_networking=OFF
    -D audacity_has_crashreports=OFF
    -D audacity_has_updates_check=OFF
    -D audacity_has_sentry_reporting=OFF
    -D audacity_lib_preference=system
    -D audacity_obey_system_dependencies=ON
    -D audacity_use_vst3sdk=system
    -S $pkgname-$_name-$pkgver
    -W no-dev
  )

  export VST3SDK='/usr/share/vst3sdk'
  export CFLAGS+=" -DNDEBUG"
  export CXXFLAGS+=" -DNDEBUG"
  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  ctest --test-dir build --output-on-failure
}

package_audacity() {
  license+=(BSD)
  groups=(pro-audio)
  depends=(
    alsa-lib libasound.so
    expat
    flac libFLAC.so libFLAC++.so
    gcc-libs
    glibc
    gdk-pixbuf2 libgdk_pixbuf-2.0.so
    glib2 libglib-2.0.so libgobject-2.0.so
    gtk3 libgdk-3.so libgtk-3.so
    hicolor-icon-theme
    jack  # likely dlopen'ed
    lame  # likely dlopen'ed
    libid3tag libid3tag.so
    libmad
    libogg libogg.so
    libsbsms libsbsms.so
    libsndfile libsndfile.so
    libsoxr
    libvorbis libvorbis.so libvorbisenc.so libvorbisfile.so
    libx11
    lilv liblilv-0.so
    mpg123 libmpg123.so
    portaudio libportaudio.so
    portmidi libportmidi.so
    portsmf libportSMF.so
    python
    soundtouch
    sqlite libsqlite3.so
    suil libsuil-0.so
    twolame libtwolame.so
    util-linux-libs
    wxwidgets-gtk3
    vamp-plugin-sdk libvamp-hostsdk.so
    wavpack
    wxwidgets-common
  )
  optdepends=(
    'audacity-docs: for documentation'
    'ffmpeg: for additional import/export capabilities'
  )
  provides=(
    ladspa-host
    lv2-host
    vamp-host
    vst-host
    vst3-host
  )


  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname-$_name-$pkgver/lib-src/libnyquist/nyquist/license.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE.nyquist"
}

package_audacity-docs() {
  license+=(CCPL)
  pkgdesc+=" - documentation"

  install -vdm 755 "$pkgdir/usr/share/doc/$pkgbase/html/"
  cp -av help/manual/* "$pkgdir/usr/share/doc/$pkgbase/html/"
}
